//
//  PhotoContainer.m
//  GoFishing
//
//  Created by apple on 16/12/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "PhotoContainer.h"
#import "UIView+SDAutoLayout.h"

@implementation PhotoContainer
{
    NSMutableArray *_imageViewsArray;
}

- (instancetype)initWithMaxItemsCount:(NSInteger)count
{
    if (self = [super init]) {
        self.maxItemsCount = count;
    }
    return self;
}

- (void)setPhotoNamesArray:(NSArray *)photoNamesArray
{
    _photoNamesArray = photoNamesArray;
    
    if (!_imageViewsArray) {
        _imageViewsArray = [NSMutableArray new];
    }
    NSInteger count = _photoNamesArray.count;

    int needsToAddItemsCount = (int)(count - _imageViewsArray.count);
    
    if (needsToAddItemsCount > 0) {
        for (int i = 0; i < needsToAddItemsCount; i++) {
            UIImageView *imageView = [UIImageView new];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
//            imageView.clipsToBounds = YES;
            [self addSubview:imageView];
            imageView.tag = i;
            [imageView addTarget:self action:@selector(click:)];
            [_imageViewsArray addObject:imageView];
        }
    }
    
    NSMutableArray *temp = [NSMutableArray new];
    
    [_imageViewsArray enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        if (idx < _photoNamesArray.count) {
            imageView.hidden = NO;
            imageView.tag = idx;
            imageView.layer.cornerRadius = 5;
            imageView.clipsToBounds = YES;
//            if (_photoNamesArray.count == 1) {
//                imageView.sd_layout.autoHeightRatio(0.622);
//            }else{
//            imageView.sd_layout.autoHeightRatio(1);
//            }
            imageView.sd_layout.autoHeightRatio(0.75);
//            [imageView sd_setImageWithURL:WebImagePath(_photoNamesArray[idx]) placeholderImage:LocalImagePath(@"图片加载失败")];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_photoNamesArray[idx]] placeholderImage:LocalImagePath(@"图片加载失败")];
        
            [temp addObject:imageView];
        } else {
            [imageView sd_clearAutoLayoutSettings];
            imageView.hidden = YES;
        }
    }];
    
//    if (_photoNamesArray.count == 1) {
//        [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:1 verticalMargin:10 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:0];
//    }else if (_photoNamesArray.count == 2) {
//        [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:2 verticalMargin:10 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:0];
//    }else{
//
//    [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:10 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:0];
//    }
    [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:12 horizontalMargin:12 verticalEdgeInset:0 horizontalEdgeInset:0];
}

- (void)click:(UITapGestureRecognizer *)image {
    if (_canBrowser) {
        
        XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:image.view.tag imageCount:self.photoNamesArray.count datasource:self];
        browser.browserStyle = XLPhotoBrowserStyleSimple;
    }
}

#pragma mark    -   XLPhotoBrowserDatasource

- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *image = [[UIImageView alloc]init];
//    [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageAddress,_photoNamesArray[index]]] placeholderImage:[UIImage imageNamed:@"图片加载失败"] options:SDWebImageAllowInvalidSSLCertificates];
    
    if ([_photoNamesArray[index] containsString:@"http"]) {
        [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_photoNamesArray[index]]] placeholderImage:[UIImage imageNamed:@"图片加载失败"] options:SDWebImageAllowInvalidSSLCertificates];
    }else{
        [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageAddress,_photoNamesArray[index]]] placeholderImage:[UIImage imageNamed:@"图片加载失败"] options:SDWebImageAllowInvalidSSLCertificates];
    }
    
    return image.image;
}

// 返回高质量图片的url
//- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
//{
//    NSString *urlStr = [[self.photoNamesArray[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//    return [NSURL URLWithString:urlStr];
//}


@end
